/*
                  MapInfo Class Implementation
*/
#include "MapInfo.h"

string MapInfo::strCurrentMap = "";
map<string, MapData*> MapInfo::Maps = map<string, MapData*>();

void MapInfo::LoadMapData(const string& strFilepath)
{
    FreeData();

    ifstream mapFile;

    mapFile.open(strFilepath.c_str());

    if (! mapFile.is_open())
    {
        cout << "Couldn't read maplist\n";

        mapFile.close();

        return;
    }

    string strLine = "";

    while (! mapFile.eof())
    {
        getline(mapFile, strLine);

        vector<string> Info = SplitString(strLine, ",");

        if (Info.size() == 3)
        {
            if (strCurrentMap == "") { strCurrentMap = Info.at(0); }

            Maps[Info.at(0)] = new MapData(Info.at(0), Info.at(1), Info.at(2));
        }
    }

    mapFile.close();
}

void MapInfo::FreeData()
{
    for (map<string, MapData*>::iterator iter = Maps.begin(); iter != Maps.end(); iter++)
    {
        delete iter->second;
    }

    Maps.clear();
}

MapData* MapInfo::GetCurrentMap()
{
    if (KeyInMap(Maps, strCurrentMap))
    {
        return Maps[strCurrentMap];
    }

    return NULL;
}

void MapInfo::SetCurrentMap(string strMap)
{
    if (! KeyInMap(Maps, strMap)) { return; }

    strCurrentMap = strMap;
}

vector<string> MapInfo::GetMapNames()
{
    vector<string> Return;

    for (map<string, MapData*>::iterator iter = Maps.begin(); iter != Maps.end(); iter++)
    {
        Return.push_back(iter->first);
    }

    return Return;
}
